package com.tronsis.distribution.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.login.LoginActivity;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.library.activity.BaseActivity;

import java.lang.ref.WeakReference;


public class SplashActivity extends BaseActivity {

    private static final String SPF_IS_LAUNCH = "spf_is_launch";
    private static final long LAUNCH_TIME = 0;//2s
    private DelayHandler mHandler ;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        mHandler = new DelayHandler(this);
        mHandler.sendEmptyMessageDelayed(0, LAUNCH_TIME);

    }


    private static class DelayHandler extends Handler {
        private WeakReference<SplashActivity> mActivity;

        public DelayHandler() {

        }

        public DelayHandler(SplashActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SplashActivity activity = mActivity.get();
            if (activity != null) {
                UserBean userBean = UserBizFactory.createBiz().getLoginUser(activity);
                Intent intent = new Intent();
                if (userBean != null) {
                    intent.setClass(activity, MainActivity.class);
                } else {
                    intent.setClass(activity, LoginActivity.class);
                }

                activity.startActivity(intent);
                activity.finish();
            }
        }
    }



}